package com.china08.yxyapi.api.manage.tec.teaching.lesson.topic;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsTopicLessonDetailController extends YxyApiBaseController {

    @Autowired
    YxyLessonsTopicLessonRepository lessonRepository;

    @Autowired
    YxyLessonsTopicCatalogRepository catalogRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/topic/lesson/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String lessonId = reqModel.lessonId;
        YxyLessonsTopicLesson topicLesson = lessonRepository.findById(lessonId);

        RespModel respModel = new RespModel();
        if (topicLesson != null) {
            String catalogId1 = topicLesson.getCatalogId1();
            String catalogId2 = topicLesson.getCatalogId2();
            String catalogId3 = topicLesson.getCatalogId3();
            Date date1 = topicLesson.getCreateDate();
            Date date2 = topicLesson.getUpDate();
            Date date3 = topicLesson.getDownDate();
            Date date4 = topicLesson.getRejectDate();
            if (date1 != null) {
                respModel.createDate = DateToolUtils.format(date1, "yyyy-MM-dd");
            }
            if (date2 != null) {
                respModel.upDate = DateToolUtils.format(date2, "yyyy-MM-dd");
            }
            if (date3 != null) {
                respModel.downDate = DateToolUtils.format(date3, "yyyy-MM-dd");
            }
            if (date4 != null) {
                respModel.rejectDate = DateToolUtils.format(date4, "yyyy-MM-dd");
            }

            respModel.lessonId = lessonId;
            respModel.name = topicLesson.getName();
            respModel.summary = topicLesson.getSummary();
            respModel.cover = topicLesson.getCover();
            respModel.rejectType = topicLesson.getRejectType();
            respModel.rejectReason = topicLesson.getRejectReason();
            respModel.downType = topicLesson.getDownType();
            respModel.downReason = topicLesson.getDownReason();
            respModel.count = topicLesson.getCount() + "";
            respModel.duration = disDuration(topicLesson.getDuration());
            respModel.pv = topicLesson.getPv() + "";
            respModel.vv = topicLesson.getVv() + "";
            respModel.status = topicLesson.getStatus();

            YxyLessonsTopicCatalog catalog1 = catalogRepository.findById(catalogId1);
            YxyLessonsTopicCatalog catalog2 = catalogRepository.findById(catalogId2);
            YxyLessonsTopicCatalog catalog3 = catalogRepository.findById(catalogId3);
            if (null != catalog1) {
                respModel.catalogName1 = catalog1.getName();
                respModel.catalogId1 = catalog1.getId();
            }
            if (null != catalog2) {
                respModel.catalogName2 = catalog2.getName();
                respModel.catalogId2 = catalog2.getId();
            }
            if (null != catalog3) {
                respModel.catalogName3 = catalog3.getName();
                respModel.catalogId3 = catalog3.getId();
            }
            if (topicLesson.getLastModifiedDate() != null) {
                respModel.lastModifiedDate = DateToolUtils.format(topicLesson.getLastModifiedDate(), "yyyy-MM-dd");
            }
        }
        return respModel;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;

        public void check() {
            Assert.isTrue(StringUtils.isNotBlank(lessonId), "课程不能为空");
        }
    }

    public static class RespModel extends YxyApiRespModel {
        public String lessonId;
        public String name;
        public String catalogId1;
        public String catalogId2;
        public String catalogId3;
        public String catalogName1 = "";
        public String catalogName2 = "";
        public String catalogName3 = "";
        public String createDate;
        public String upDate;
        public String downDate;
        public String rejectDate;
        public String summary;
        public String cover;
        public String lastModifiedDate;
        public String rejectType;
        public String rejectReason;
        public String downType;
        public String downReason;
        public String count;
        public String duration;
        public String vv;
        public String pv;
        public Integer status;
    }

    public String disDuration(Integer miao) {
        if (miao == null || miao < 0) {
            miao = 0;
        }
        int h = miao / 3600;
        int m = (miao % 3600) / 60;
        int s = (miao % 3600) % 60;
        if (h > 0) {
            return (h + "时" + m + "分" + s + "秒");
        } else {
            if (m > 0) {
                return (m + "分" + s + "秒");
            } else {
                return (s + "秒");
            }
        }
    }
}
